package com.kaesar.leetcode.LeetCode_051_100;

public class LeetCode_064 {
    private static int result = Integer.MAX_VALUE;

    /**
     * 穷举法 递归
     *
     * @param grid
     * @return
     */
    public static int minPathSum(int[][] grid) {
        minPathSum(grid, 0, 0, 0);
        return result;
    }

    private static void minPathSum(int[][] grid, int x, int y, int sum) {
        sum += grid[x][y];
        // 走到右下角的单元格
        if (x == grid.length - 1 && y == grid[0].length - 1) {
            result = Math.min(sum, result);
            return;
        }
        if (x == grid.length - 1) {
            // 走到最后一行，往右走
            minPathSum(grid, x, y + 1, sum);
        } else if (y == grid[0].length - 1) {
            // 走到最后一列，往下走
            minPathSum(grid, x + 1, y, sum);
        } else {
            // 往下走
            minPathSum(grid, x, y + 1, sum);
            // 往右走
            minPathSum(grid, x + 1, y, sum);
        }
    }

//    /**
//     * @param grid
//     * @return
//     */
//    public static int minPathSum2(int[][] grid) {
//
//    }

    public static void main(String[] args) {
        int[][] grid = new int[][]{{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
        System.out.println(minPathSum(grid));
    }
}
